package edu.tsinghua.ddb.util;

import java.lang.reflect.Field;

public class Sql {
	public static Integer mapType(String type_str) throws Exception {
		Class<?> types;
		try {
			types = Class.forName("java.sql.Types");
			Field f = types.getField(type_str.toUpperCase());
			return (Integer) f.get(null);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
			throw new Exception("type_str illegal");
		}
		return null;
	}

	public static String mapType(Integer type_int) {
		Class<?> types;
		try {
			types = Class.forName("java.sql.Types");
			Field[] fields = types.getFields();
			for (Field f : fields) {
				if (type_int.equals(f.get(null)))
					return f.getName();
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		throw new Error("type_int illegal");
	}
}
